#!/bin/bash

set -e



if [[ "$OSTYPE" == "darwin"* ]]; then # MAC
  exit
  if [ ! -d "/Applications/Postgres.app/Contents/Versions/11" ]; then
    # updating brew takes forver
    # also lock to specific version of postgres.app
    HOMEBREW_NO_AUTO_UPDATE=1 brew cask install \
      https://raw.githubusercontent.com/Homebrew/homebrew-cask/b52633190d94bbffd11cbef9168164db58c658a3/Casks/postgres.rb
  else
    echo "Postgres.app aleady installed"
  fi

  yarn rebuild_ext

  echo "Updating node_modules/libpq/build/Release/addon.node"
  install_name_tool -change \
    /Applications/Postgres.app/Contents/Versions/11/lib/libpq.5.dylib \
    @loader_path/../../../../../../vendor/darwin/libpq.5.11.dylib \
    node_modules/libpq/build/Release/addon.node
  otool -L node_modules/libpq/build/Release/addon.node

elif [[ "$OSTYPE" == "linux-gnu" ]]; then # LINUX
  yarn rebuild_ext

  sudo apt-get clean > /dev/null
  sudo apt-get update > /dev/null
  sudo apt-get install -q rpm bsdtar
#else
  # skip for windows
fi
